﻿
namespace bookMaster.AssetSystem.Windows.Forms
{
	/// <summary>
	/// abstract Libros typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class LibrosDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<LibrosDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Libros _currentLibros = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxLibrosDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxLibrosErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxLibrosBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the IdLibro property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIdLibroDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the IsbnLibro property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIsbnLibroDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the NombreLibro property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxNombreLibroDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the CantidadExistenciaLibro property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxCantidadExistenciaLibroDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the CantidadEntranteLibro property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxCantidadEntranteLibroDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Libros> _LibrosList;
				
		/// <summary> 
		/// The list of Libros to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Libros> LibrosList
		{
			get {return this._LibrosList;}
			set
			{
				this._LibrosList = value;
				this.uxLibrosBindingSource.DataSource = null;
				this.uxLibrosBindingSource.DataSource = value;
				this.uxLibrosDataGridView.DataSource = null;
				this.uxLibrosDataGridView.DataSource = this.uxLibrosBindingSource;				
				//this.uxLibrosBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxLibrosBindingSource_ListChanged);
				this.uxLibrosBindingSource.CurrentItemChanged += new System.EventHandler(OnLibrosBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnLibrosBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentLibros = uxLibrosBindingSource.Current as Entities.Libros;
			
			if (_currentLibros != null)
			{
				_currentLibros.Validate();
			}
			//_Libros.Validate();
			OnCurrentEntityChanged();
		}

		//void uxLibrosBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Libros"/> instance.
		/// </summary>
		public Entities.Libros SelectedLibros
		{
			get {return this._currentLibros;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxLibrosDataGridView.VirtualMode;}
			set
			{
				this.uxLibrosDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxLibrosDataGridView.AllowUserToAddRows;}
			set {this.uxLibrosDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxLibrosDataGridView.AllowUserToDeleteRows;}
			set {this.uxLibrosDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxLibrosDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxLibrosDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="LibrosDataGridViewBase"/> class.
		/// </summary>
		public LibrosDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxLibrosDataGridView = new System.Windows.Forms.DataGridView();
			this.uxLibrosBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxLibrosErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIdLibroDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIsbnLibroDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxNombreLibroDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxCantidadExistenciaLibroDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxCantidadEntranteLibroDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.uxLibrosDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxLibrosBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxLibrosErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxLibrosErrorProvider
			// 
			this.uxLibrosErrorProvider.ContainerControl = this;
			this.uxLibrosErrorProvider.DataSource = this.uxLibrosBindingSource;						
			// 
			// uxLibrosDataGridView
			// 
			this.uxLibrosDataGridView.AutoGenerateColumns = false;
			this.uxLibrosDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxLibrosDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIdLibroDataGridViewColumn,
		this.uxIsbnLibroDataGridViewColumn,
		this.uxNombreLibroDataGridViewColumn,
		this.uxCantidadExistenciaLibroDataGridViewColumn,
		this.uxCantidadEntranteLibroDataGridViewColumn			});
			this.uxLibrosDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxLibrosDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxLibrosDataGridView.Name = "uxLibrosDataGridView";
			this.uxLibrosDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxLibrosDataGridView.TabIndex = 0;	
			this.uxLibrosDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxLibrosDataGridView.EnableHeadersVisualStyles = false;
			this.uxLibrosDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnLibrosDataGridViewDataError);
			this.uxLibrosDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnLibrosDataGridViewCellValueNeeded);
			this.uxLibrosDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnLibrosDataGridViewCellValuePushed);
			
			//
			// uxIdLibroDataGridViewColumn
			//
			this.uxIdLibroDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdLibroDataGridViewColumn.DataPropertyName = "IdLibro";
			this.uxIdLibroDataGridViewColumn.HeaderText = "IdLibro";
			this.uxIdLibroDataGridViewColumn.Name = "uxIdLibroDataGridViewColumn";
			this.uxIdLibroDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdLibroDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdLibroDataGridViewColumn.ReadOnly = true;		
			//
			// uxIsbnLibroDataGridViewColumn
			//
			this.uxIsbnLibroDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIsbnLibroDataGridViewColumn.DataPropertyName = "IsbnLibro";
			this.uxIsbnLibroDataGridViewColumn.HeaderText = "IsbnLibro";
			this.uxIsbnLibroDataGridViewColumn.Name = "uxIsbnLibroDataGridViewColumn";
			this.uxIsbnLibroDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIsbnLibroDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIsbnLibroDataGridViewColumn.ReadOnly = false;		
			//
			// uxNombreLibroDataGridViewColumn
			//
			this.uxNombreLibroDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxNombreLibroDataGridViewColumn.DataPropertyName = "NombreLibro";
			this.uxNombreLibroDataGridViewColumn.HeaderText = "NombreLibro";
			this.uxNombreLibroDataGridViewColumn.Name = "uxNombreLibroDataGridViewColumn";
			this.uxNombreLibroDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxNombreLibroDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxNombreLibroDataGridViewColumn.ReadOnly = false;		
			//
			// uxCantidadExistenciaLibroDataGridViewColumn
			//
			this.uxCantidadExistenciaLibroDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxCantidadExistenciaLibroDataGridViewColumn.DataPropertyName = "CantidadExistenciaLibro";
			this.uxCantidadExistenciaLibroDataGridViewColumn.HeaderText = "CantidadExistenciaLibro";
			this.uxCantidadExistenciaLibroDataGridViewColumn.Name = "uxCantidadExistenciaLibroDataGridViewColumn";
			this.uxCantidadExistenciaLibroDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxCantidadExistenciaLibroDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxCantidadExistenciaLibroDataGridViewColumn.ReadOnly = false;		
			//
			// uxCantidadEntranteLibroDataGridViewColumn
			//
			this.uxCantidadEntranteLibroDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxCantidadEntranteLibroDataGridViewColumn.DataPropertyName = "CantidadEntranteLibro";
			this.uxCantidadEntranteLibroDataGridViewColumn.HeaderText = "CantidadEntranteLibro";
			this.uxCantidadEntranteLibroDataGridViewColumn.Name = "uxCantidadEntranteLibroDataGridViewColumn";
			this.uxCantidadEntranteLibroDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxCantidadEntranteLibroDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxCantidadEntranteLibroDataGridViewColumn.ReadOnly = false;		
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxLibrosDataGridView);
			this.Name = "LibrosDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			((System.ComponentModel.ISupportInitialize)(this.uxLibrosErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxLibrosDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxLibrosBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				LibrosDataGridViewEventArgs args = new LibrosDataGridViewEventArgs();
				args.Libros = _currentLibros;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class LibrosDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Libros	_Libros;
	
			/// <summary>
			/// the  Entities.Libros instance.
			/// </summary>
			public Entities.Libros Libros
			{
				get { return _Libros; }
				set { _Libros = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxLibrosDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnLibrosDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxLibrosDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnLibrosDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxLibrosDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdLibroDataGridViewColumn":
						e.Value = LibrosList[e.RowIndex].IdLibro;
						break;
					case "uxIsbnLibroDataGridViewColumn":
						e.Value = LibrosList[e.RowIndex].IsbnLibro;
						break;
					case "uxNombreLibroDataGridViewColumn":
						e.Value = LibrosList[e.RowIndex].NombreLibro;
						break;
					case "uxCantidadExistenciaLibroDataGridViewColumn":
						e.Value = LibrosList[e.RowIndex].CantidadExistenciaLibro;
						break;
					case "uxCantidadEntranteLibroDataGridViewColumn":
						e.Value = LibrosList[e.RowIndex].CantidadEntranteLibro;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxLibrosDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnLibrosDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxLibrosDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdLibroDataGridViewColumn":
						LibrosList[e.RowIndex].IdLibro = (System.Int32)e.Value;
						break;
					case "uxIsbnLibroDataGridViewColumn":
						LibrosList[e.RowIndex].IsbnLibro = (System.String)e.Value;
						break;
					case "uxNombreLibroDataGridViewColumn":
						LibrosList[e.RowIndex].NombreLibro = (System.String)e.Value;
						break;
					case "uxCantidadExistenciaLibroDataGridViewColumn":
						LibrosList[e.RowIndex].CantidadExistenciaLibro = (System.Int32)e.Value;
						break;
					case "uxCantidadEntranteLibroDataGridViewColumn":
						LibrosList[e.RowIndex].CantidadEntranteLibro = (System.Int32?)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
